package com.efesco.crm.quotation;

import com.bstek.dorado.data.*;
import com.bstek.dorado.common.*;
import com.bstek.dorado.common.rtexpr.impl.ExpressionUtils;
import com.bstek.dorado.data.db.*;
import com.bstek.dorado.utils.variant.VariantSet;
import com.efesco.common.*;
import java.util.*;

/**
 * quotationTemplate_datasetItemsListener
 */
public class quotationTemplate_datasetItemsListener extends AbstractDatasetListener {
  public boolean beforeLoadData(Dataset dataset) throws Exception {

    return true;
  }

  public void afterLoadData(Dataset dataset)throws Exception {
	 DoradoContext context = DoradoContext.getContext();
	 String today=ExpressionUtils.getInstance().getDate("yyyyMM");
	 String templateId=context.getParameter("quotation_template_id");
	 //System.out.println("************************"+dataset.parameters().getShort("theTime"));
	 DBStatement statement=new DBStatement();
	 //SqlDataset ds=new SqlDataset();
	 String sql="select is_detail from s_quotation_template where quotation_template_id="+templateId;
	 statement.setSql(sql);	 
	 VariantSet varIsDetail=statement.query();
	 statement.close();
	 //if(varIsDetail.getString(0).equals("1"))
	 
	 sql="SELECT s_product.PRODUCT_ID,s_product.PRODUCT_NAME,s_product.PRODUCT_PRICE,"
		+"s_product.PRODUCT_DETAIL_DESC,s_product.PRODUCT_SIMPLE_DESC,s_product_type.PRODUCT_TYPE_ID,"
		+"s_product_type.TYPE_CLASS,s_product_type.TYPE_CATEGORY,s_benefit_ratio.ENTERPRISE_RATIO,"
		+"s_benefit_ratio.PERSON_RATIO FROM s_product,s_product_type s_product_type,s_benefit_ratio s_benefit_ratio "
		+"WHERE (s_product.PRODUCT_TYPE_ID = s_product_type.PRODUCT_TYPE_ID(+)) "
		+"AND (s_product.PRODUCT_ID = s_benefit_ratio.PRODUCT_ID(+)) AND((s_product.IS_ENABLED = '1') "
		+"and ( (s_benefit_ratio.effect_start_month<="+today+" "
		+"and (s_benefit_ratio.effect_stop_month is null or s_benefit_ratio.effect_stop_month>="+today+")) "
		+"or (s_benefit_ratio.effect_start_month is null) ))"
		+"and exists(select 'x' from s_quotation_template_item qti where qti.product_id=s_product.PRODUCT_ID "
		+"and qti.quotation_template_id="+templateId+")";
	 /*ds.setSql(sql);
	 ds.load(true);
	 ds.getField("ENTERPRISE_RATIO").setDataType("double");
	 ds.moveFirst();
	 while(!ds.isLast()){
		 System.out.println(ds.getString("ENTERPRISE_RATIO")+"&&&&&&&&&&&&&&&"+ds.getDouble("PERSON_RATIO"));
		 ds.moveNext();
	 }*/
	 statement.setSql(sql);
	 List list=statement.queryForList();
	 	 
	 dataset.moveFirst();
	 while(!dataset.isLast()){
		 for(int i=0;i<list.size();i++){
			 VariantSet var=(VariantSet)list.get(i);
			 if(dataset.getString("product_id").equals(var.getString("PRODUCT_ID"))){
				 dataset.setValue("select",Boolean.TRUE);
				 dataset.setValue("PRODUCT_NAME",var.getString("PRODUCT_NAME"));
				 dataset.setValue("PRODUCT_PRICE",var.getString("PRODUCT_PRICE"));
				 //String eRatio=Tools.toStr(ds.getDouble("ENTERPRISE_RATIO"));
				 //String pRatio=Tools.toStr(ds.getDouble("PERSON_RATIO"));
				 
				 double dERatio=Tools.format(var.getDouble("ENTERPRISE_RATIO"),4);
				 double dPRatio=Tools.format(var.getDouble("PERSON_RATIO"),4);
				 //System.out.println(dERatio+"%%%%%%%%%%%%%%%%%"+dPRatio);				 
				 if(dERatio+dPRatio==0){
					 dataset.setValue("ENTERPRISE_RATIO","");
					 dataset.setValue("PERSON_RATIO","");
					 dataset.setValue("ratio","");
				 }else{
					 dataset.setValue("ENTERPRISE_RATIO",Tools.format(dERatio*100,2)+"%");
					 dataset.setValue("PERSON_RATIO",Tools.format(dPRatio*100,2)+"%");
					 dataset.setValue("ratio",Tools.format((dERatio+dPRatio)*100,2)+"%");
				 }
				 String isDetail=Tools.toStr(varIsDetail.getString(0));
				 if(varIsDetail!=null && isDetail.equals("1")) 
					 dataset.setValue("memo",var.getString("PRODUCT_DETAIL_DESC"));
				 else
					 dataset.setValue("memo",var.getString("PRODUCT_SIMPLE_DESC"));
				 //dataset.setValue("PRODUCT_NAME",ds.getString("PRODUCT_NAME"));
				 dataset.setValue("TYPE_CLASS",var.getString("TYPE_CLASS"));
				 dataset.setValue("TYPE_CATEGORY",var.getString("TYPE_CATEGORY"));
				 
				 break;
			 }			 
		 }
		 
		 dataset.moveNext();
	 }	 
     
  }

}
